package com.taoyige.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {
	
	public static long getIntervalTime(String endTime, String startTime) {
		long quot = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			 Date date1 = ft.parse( startTime );
			 Date date2 = ft.parse( endTime );
			quot = (date2.getTime() - date1.getTime())/1000;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return quot;
	}
	
	public static long getInterval(Date endTime, Date startTime) {
		long quot = 0;
		// SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// Date date1 = ft.parse( time1 );
			// Date date2 = ft.parse( time2 );
			quot = endTime.getTime() - startTime.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return quot;
	}

	public static String getNowTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		// cal.add(Calendar.DATE, 1);//solr的时间格式决定了这要多加上一天
		Date date = cal.getTime();
		return df.format(date);

	}

	public static String getNextMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 1);// solr的时间格式决定了这要多加上一天
		Date date = cal.getTime();
		return df.format(date);

	}

	public static String getWhenBeforeTime(int ds) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -ds);
		Date date = cal.getTime();
		return df.format(date);
	}

	// 判断日期+runnyingMonths是否在当前时间之后
	public static boolean isAfterNow(Date date, int runningMonths) {
		Date now = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, runningMonths);
		return now.before(cal.getTime());
	}

	public static Date addMonths(Date date, int runningMonths) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, runningMonths);
		return cal.getTime();
	}
	
	public static Date addHours(Date date, int runningHours) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, runningHours);
		return cal.getTime();
	}
	
	public static Date addDates(Date date, int runningDates) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, runningDates);
		return cal.getTime();
	}
	
	public static Date parse(String d,String format) {
		Date da=null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat(format);
			da=sf.parse(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return da;
	}
	
	public static Date parse(String d) {
		Date da=null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			da=sf.parse(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return da;
	}
	
	public static String formatDate(Date d) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(d);
	}
	
	public static String formatHourTime(Date d) {
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
		return sf.format(d);
	}
	
	public static String formatMinutesTime(Date d) {
		SimpleDateFormat sf = new SimpleDateFormat("mm:ss");
		return sf.format(d);
	}
	
	public static String format(Date d) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sf.format(d);
	}


}
